import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/Home.vue'
import local from '../utils/local'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {

  return originalPush.call(this, location).catch(err => err)

}
const routes = [
  {
    path: '/',
    redirect: '/home',
    component: Home
  },
  {
    path: '/home',
    component: Home,
    redirect: '/dashboard',
    children: [
      // 后台首页
      {
        path: '/dashboard',
        component: () => import(/* webpackChunkName: "首页" */ '../views/Dashboard/Dashboard.vue')
      },
    ]
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Logins.vue')
  },

  // 错误 404 页面
  {
    path: '*',
    redirect: '/404'
  }, {
    path: '/404',
    component: () => import(/* webpackChunkName: "404" */ '../views/Error.vue')
  }


  // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

]


// 这是提取出来的路由，为了做权限用
const syncRoute = {
  path: '/home',
  component: Home,
  redirect: '/dashboard',
  children: [
    // 后台首页
    {
      meta: { name: '后台首页', icon: 'icon-shouye' },
      path: '/dashboard',
      visible: true,
      component: () => import(/* webpackChunkName: "首页" */ '../views/Dashboard/Dashboard.vue')

    },
    // 订单管理
    {
      path: '/Order',
      visible: true,
      meta: { name: '订单管理', role: ['super', 'normal'], icon: 'icon-icon-' },
      redirect: '/Order/list',
      component: () => import(/* webpackChunkName: "首页" */ '../views/Order/index.vue'),
      children: [
        { path: '/Order/list', visible: true, meta: { name: '订单列表' }, component: () => import(/* webpackChunkName: "首页" */ '../views/Order/orderlist.vue') },
        { path: '/Order/edit', visible: false, meta: { name: '订单编辑', role: ['super'] }, component: () => import(/* webpackChunkName: "首页" */ '../views/Order/orderedit.vue') }
      ]
    },
    // 店铺管理
    {
      path: '/Shop',
      visible: true,
      meta: { name: '店铺管理', role: ['super'], icon: 'icon-dianpu' },
      component: () => import(/* webpackChunkName: "首页" */ '../views/Shop/index.vue')

    },
    // Por   商品列表  
    {
      path: '/pro',
      visible: true,
      redirect: '/pro/list',
      meta: { name: '商品管理', icon: 'icon-shangpin' },
      component: () => import(/* webpackChunkName: "首页" */ '../views/Product/Index.vue'),
      children: [
        {
          path: '/pro/list',
          visible: true,
          meta: { name: '商品列表' },
          component: () => import(/* webpackChunkName: "首页" */ '../views/Product/proList.vue'),
        },
        {
          path: '/pro/add',
          visible: true,
          meta: { name: '商品添加' },

          component: () => import(/* webpackChunkName: "首页" */ '../views/Product/proAdd.vue'),
        }, {
          path: '/pro/cate',
          visible: true,
          meta: { name: '商品分类' },

          component: () => import(/* webpackChunkName: "首页" */ '../views/Product/proCate.vue'),
        },
        {
          path: '/pro/edit',
          visible: false,
          meta: { name: '商品编辑' },
          component: () => import(/* webpackChunkName: "首页" */ '../views/Product/proEdit.vue'),
        }
      ]
    },

    // 账号管理
    {
      path: '/account',
      visible: true,
      meta: { name: '账号管理', role: ['super', 'normal'], icon: 'icon-zhanghao' },
      redirect: '/account/list',
      component: () => import(/* webpackChunkName: "首页" */ '../views/Account/Index.vue'),
      children: [
        {
          path: '/account/list',
          visible: true,
          meta: { name: '账号列表', role: ['super'] },

          component: () => import(/* webpackChunkName: "首页" */ '../views/Account/AccountList.vue'),
        },
        {
          path: '/account/add',
          visible: true,
          meta: { name: '账号添加', role: ['super'] },

          component: () => import(/* webpackChunkName: "首页" */ '../views/Account/AccountAdd.vue'),
        },
        {
          path: '/account/pwd',
          visible: true,
          meta: { name: '修改密码' },

          component: () => import(/* webpackChunkName: "首页" */ '../views/Account/ResetPwd.vue'),
        },
        {
          visible: false,
          path: '/account/personal',
          meta: { name: '个人中心' },

          component: () => import(/* webpackChunkName: "首页" */ '../views/Account/Personal.vue'),
        },
      ]
    },

    // 销售统计
    {
      path: '/sell',
      visible: true,
      redirect: '/sell/goods',
      meta: { name: '销售统计', role: ['super'], icon: 'icon-tongji' },

      component: () => import(/* webpackChunkName: "首页" */ '../views/SellCount/Index.vue'),
      children: [
        {
          path: '/sell/goods',
          visible: true,
          meta: { name: '商品统计' },

          component: () => import(/* webpackChunkName: "首页" */ '../views/SellCount/GoodsCount.vue'),
        }, {
          path: '/sell/order',
          visible: true,
          meta: { name: '订单统计' },

          component: () => import(/* webpackChunkName: "首页" */ '../views/SellCount/OrdersCount.vue'),
        }
      ]
    }
  ]
};

const router = new VueRouter({
  routes
})



// 导航守卫  路由直接的跳转，判断有没有token
router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})


// 声明一个新路由函数 //调用一下
createRoute()
// 去做这个过滤---权限
export function createRoute() {
  const role = local.get('role')
  if (!role) { return }
  const finalArr = calcRoute(syncRoute.children, role)
  syncRoute.children = finalArr
  // 把过滤封装出去

  // 加入路由的路线上去   必须是数组
  router.addRoutes([syncRoute])

  // 左边的菜单栏根据role的角色而不一样
  const finallMenu = calcMenu(finalArr)
  local.set('menu', JSON.stringify(finallMenu))
}
// 菜单栏的过滤函数
function calcMenu(arr) {
  return arr.filter(item => {
    if (item.visible) {
      if (item.children && item.children.length > 0) {
        item.children = calcMenu(item.children)
      }
      return true
    } else {
      return false
    }
  })
}


// 这是过滤  里面有syncRoute里面的children数据  通过过滤去返回新的数组，符合当前角色可以访问的路由
function calcRoute(arr, role) {
  return arr.filter(item => {
    if (hasPermission(item, role)) {
      // 一级路由已经通过，现在判断二级路由
      if (item.children && item.children.length > 0) {
        //  二级路由筛选出符合角色的路由，赋值到一级数组中，因为最后返回的是一级也就是第一个数组
        item.children = calcRoute(item.children, role)
      }
      return true
    } else {
      return false
    }
  })
}

// 鉴定权限的方法
function hasPermission(o, r) {
  if (o.meta && o.meta.role) {
    return o.meta.role.includes(r)
  } else {
    return true
  }
}

export default router
